#ifndef _WIN32
#include <time.h>
#include "Timing.h"

typedef unsigned long int mulong;
// -----------------------------------------------------------------------
Timing::Timing():lastTick(0),elapsedTime(0.0),paused(true)
{
#ifdef WIN32
        LARGE_INTEGER freq;
        if ( QueryPerformanceFrequency(&freq) && ( freq.QuadPart >= 1000 ) )
        {

        }
#else
#endif
    lastTick = static_cast<mulong>( getSystemTime() );
}
// -----------------------------------------------------------------------
Timing::~Timing()
{

}
// -----------------------------------------------------------------------
void Timing::pause()
{
    paused = true;
    elapsedTime = 0.0;
}
// -----------------------------------------------------------------------
void Timing::resume(void)
{
    paused = false;
    elapsedTime = 0.0;

    lastTick = static_cast<mulong>( getSystemTime() );
}
// -----------------------------------------------------------------------
void Timing::tick(void)
{
    double now = getSystemTime();
    elapsedTime = ( now - lastTick ) * 1.0;
    lastTick = now;
}
// -----------------------------------------------------------------------
double Timing::getElapsedTime(void)
{
	if( paused)
		return 0.0;

	double now = getSystemTime();
    return (now - lastTick);
}
// -----------------------------------------------------------------------
double Timing::getMilliseconds(void)
{
    return (getSystemTime() - lastTick)*1000;
}
// -----------------------------------------------------------------------
Timing& Timing::getInstance(void)
{
    static Timing theTiming;
    return theTiming;
}
// -----------------------------------------------------------------------
double Timing::getSystemTime(void)
{
	timespec ts;
    clock_gettime(CLOCK_REALTIME, &ts);

    return (ts.tv_sec)/1.0 + (ts.tv_nsec)/1000000000.0;
}
// -----------------------------------------------------------------------

#endif
